package gd.qaq.blue.bluespick;

import com.gmail.filoghost.holographicdisplays.api.Hologram;
import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
import gd.qaq.blue.bluespick.api.PickAPI;
import gd.qaq.blue.bluespick.command.BluesPickCommands;
import gd.qaq.blue.bluespick.dto.AreaData;
import gd.qaq.blue.bluespick.dto.PickBlockData;
import gd.qaq.blue.bluespick.entity.PickHologram;
import gd.qaq.blue.bluespick.entity.PickingTask;
import gd.qaq.blue.bluespick.listener.AreaListener;
import gd.qaq.blue.bluespick.util.JDBCUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static gd.qaq.blue.bluespick.util.StringUtil.*;

public class BluesPick extends JavaPlugin {
    public static JDBCUtils jdbcUtils;
    public static List<AreaData> areas;
    public static FileConfiguration config;
    public static BluesPick plugin;
    public static Map<String, PickHologram> holograms = new HashMap<>();
    public static Map<String, PickingTask> pickingPlayers = new HashMap<>();
    public static Map<String, Player> pickingBlock = new HashMap<>();
    @Override
    public void onEnable() {
        plugin = this;
        initConfigAndDatabase();
        initData();
        reloadHolograms();
        Bukkit.getPluginCommand("bluespick").setExecutor(new BluesPickCommands());
        Bukkit.getPluginManager().registerEvents(new AreaListener(), this);
    }

    public static void reloadHolograms(){
        //重置全息文本
        HologramsAPI.getHolograms(plugin).forEach(Hologram::delete);
        //从数据库读取采集物位置并重新创建全息文本
        List<PickBlockData> allPick = jdbcUtils.getAllPick();
        //遍历采集物种
        for (PickBlockData pickBlockData : allPick) {
            //获取采集物种其中一个的位置
            Location location = new Location(plugin.getServer().getWorld( pickBlockData.getWorld()), pickBlockData.getX(), pickBlockData.getY(), pickBlockData.getZ());
            //通过该位置方块去获取所有该采集区域中相同id的方块
            PickBlockData pickBlock = PickAPI.isPickBlock(location.getBlock());
            List<Block> allBlocks = pickBlock.getAreaData().getArea().getAllBlocks(Integer.parseInt(pickBlock.getPickItemId().split(":")[0]), Byte.parseByte(pickBlockData.getPickItemId().split(":")[1]));
            //为所有方块添加全息图
            for (Block allBlock : allBlocks) {
                Location location1 = allBlock.getLocation();
                Hologram hologram = HologramsAPI.createHologram(plugin, new Location(location1.getWorld(),
                        location1.getX() + config.getDouble("hologram.offset.x"),
                        location1.getY() + config.getDouble("hologram.offset.y"),
                        location1.getZ() + config.getDouble("hologram.offset.z")));
                hologram.appendTextLine(colorFormat(pickBlockData.getPickName().replace(";;"," ")+" &7&l||||||||||"));
                //把所有的采集物和他的全息图关联起来
                PickHologram pickHologram = new PickHologram();
                pickHologram.setHologram(hologram);
                pickHologram.setBlock(allBlock);
                holograms.put(location1.getWorld().getName()+"&&"+
                        location1.getX()+"&&"+location1.getY()+"&&"+location1.getZ(),pickHologram);
                plugin.getLogger().info("已添加全息图: "+pickBlock.getPickItemId()+"&&"+location1.getWorld().getName()+"&&"+
                        location1.getX()+"&&"+location1.getY()+"&&"+location1.getZ());
            }
        }
    }

    private void initData() {
        List<AreaData> areas = jdbcUtils.getAreas();
        BluesPick.areas = areas == null ? new ArrayList<>() : areas;
    }

    private void initConfigAndDatabase() {
        this.saveDefaultConfig();
        config = this.getConfig();
        jdbcUtils = new JDBCUtils("plugins\\BluesPick\\data.db");
        jdbcUtils.isShow = true;
    }

    public void reload(Player p) {
        if (p.isOp()) {
            try {
                this.reloadConfig();
                config = this.getConfig();
                onEnable();
                success(p, "插件重载成功");
            } catch (Exception e) {
                e.printStackTrace();
                error(p, "插件重载失败!");
            }
        }
    }

}
